<?php
namespace App\Common\Output;

use App\Common\ErrorCode\ErrorCode;
use Illuminate\Http\JsonResponse;

/**
 * Class JsonOutput
 * @package App\Common
 */
class JsonOutput implements OutputInterface
{
    public static function result(array $data = [], $status = JsonResponse::HTTP_OK, $header = [])
    {
//        $header['Access-Control-Allow-Origin'] = '*';
        $json = JsonResponse::create($data, $status, $header);
        $json->send();exit;
    }

    public static function success(array $data = [])
    {
        $resultData = [
            'code' => ErrorCode::ERROR_SUCCESS,
            'message' => ErrorCode::codeToMessage(ErrorCode::ERROR_SUCCESS),
        ];
        $resultData['data'] = $data;
        self::result($resultData);
    }

    /**
     * @param int $code
     * @param string $message
     * @param int $status http状态码
     */
    public static function error(int $code, string $message = '', $status = JsonResponse::HTTP_OK)
    {
        if ($message === '') {
            $message = ErrorCode::codeToMessage($code);
        }
        self::result([
            'code' => $code,
            'message' => $message
        ], $status);
    }
}